package com.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;

import com.androidlibview.R;

/**
 * 
 * 正在加载的布局封装<br>
 * <b>在进如主页面的时候可以使用，先显示正在加载中，当获取到api所有数据的时候，在显示页面</b><br>
 * <b>如果联网失败的时候可以显示，网络错误,等自定义错误页面</b>
 * 
 * @author 韩伟光
 * @version 1.0
 * @since 2016-1-18
 */
public class LoadingLayout extends FrameLayout {

    private int emptyView, errorView, loadingView;
    private OnClickListener onRetryClickListener;

    public LoadingLayout(Context context) {
        this(context, null);
    }

    public LoadingLayout(Context context, AttributeSet attrs) {
        this(context, attrs, -1);
    }

    public LoadingLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        TypedArray a =
                        context.getTheme().obtainStyledAttributes(attrs, R.styleable.LoadingLayout,
                                        0, 0);
        try {
            emptyView = a.getResourceId(R.styleable.LoadingLayout_emptyView, R.layout.empty_view);
            errorView = a.getResourceId(R.styleable.LoadingLayout_errorView, R.layout.error_view);
            loadingView =
                            a.getResourceId(R.styleable.LoadingLayout_loadingView,
                                            R.layout.loading_view);

            LayoutInflater inflater = LayoutInflater.from(getContext());
            inflater.inflate(emptyView, this, true);
            inflater.inflate(errorView, this, true);
            inflater.inflate(loadingView, this, true);
        } finally {
            a.recycle();
        }
    }

    protected void onFinishInflate() {
        super.onFinishInflate();
        for (int i = 0; i < getChildCount() - 1; i++) {
            getChildAt(i).setVisibility(GONE);
        }
        
        
        View erro = findViewById(R.id.btn_error_retry);
        /* 设置点击事件 */
        if (erro != null) {
            erro.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onRetryClickListener != null) {
                        onRetryClickListener.onClick(v);
                    }

                }
            });
        }
        View empty = findViewById(R.id.btn_empty_retry);
        /*点击事件*/
        if (empty != null) {
            empty.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onRetryClickListener != null) {
                        onRetryClickListener.onClick(v);
                    }

                }
            });
        }

    }

    public void setOnRetryClickListener(OnClickListener onRetryClickListener) {
        this.onRetryClickListener = onRetryClickListener;
    }

    public void showEmpty() {
        for (int i = 0; i < this.getChildCount(); i++) {
            View child = this.getChildAt(i);
            if (i == 0) {
                child.setVisibility(VISIBLE);
            } else {
                child.setVisibility(GONE);
            }
        }
    }

    public void showError() {
        for (int i = 0; i < this.getChildCount(); i++) {
            View child = this.getChildAt(i);
            if (i == 1) {
                child.setVisibility(VISIBLE);
            } else {
                child.setVisibility(GONE);
            }
        }
    }

    public void showLoading() {
        for (int i = 0; i < this.getChildCount(); i++) {
            View child = this.getChildAt(i);
            if (i == 2) {
                child.setVisibility(VISIBLE);
            } else {
                child.setVisibility(GONE);
            }
        }
    }

    public void showContent() {
        for (int i = 0; i < this.getChildCount(); i++) {
            View child = this.getChildAt(i);
            if (i == 3) {
                child.setVisibility(VISIBLE);
            } else {
                child.setVisibility(GONE);
            }
        }
    }
}
